package com.nononsenseapps.feeder.ui.home

import android.view.ContextMenu
import android.view.MenuInflater
import android.view.View
import android.widget.TextView
import com.nononsenseapps.feeder.ui.R
import com.nononsenseapps.feeder.db.room.ID_ALL_FEEDS
import com.nononsenseapps.feeder.util.DateUtils
import java.util.Calendar

class AllFeedHolder(private val onNavigationItemClickListener: OnNavigationItemClickListener, v: View) : androidx.recyclerview.widget.RecyclerView.ViewHolder(v), View.OnClickListener, View.OnCreateContextMenuListener {
    val title: TextView = v.findViewById(R.id.feed_name)
    val unreadCount: TextView = v.findViewById(R.id.feed_unreadcount)

    init {
        title.setText(R.string.all_feeds)
        v.setOnClickListener(this)
        v.setOnCreateContextMenuListener(this)
    }

    /**
     * Called when a view has been clicked.
     *
     * @param v The view that was clicked.
     */
    override fun onClick(v: View) {
        onNavigationItemClickListener.onNavigationItemClick(
                ID_ALL_FEEDS, null, null, null
        )
    }

    override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenu.ContextMenuInfo?) {
        v?.context?.let { context ->
            if (menu != null) {
                MenuInflater(context).inflate(R.menu.contextmenu_feed_all, menu)

                menu.findItem(R.id.action_toggle_feed_read_able).setOnMenuItemClickListener {
                     onNavigationItemClickListener.toggleFeedReadable(ID_ALL_FEEDS,true)
                    true
                }
                menu.findItem(R.id.action_toggle_feed_unread_able).setOnMenuItemClickListener {
                    onNavigationItemClickListener.toggleFeedReadable(ID_ALL_FEEDS,false)
                    true
                }
                /*menu.findItem(R.id.action_sync_feed_unread_able).setOnMenuItemClickListener {
                    true
                }*/

                val feedId= ID_ALL_FEEDS
                menu.findItem(R.id.action_before_read_hour).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_HOUR)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_today).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_DAY)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_three_day).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_THREE_DAY)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_weak).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_WEEK)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_month).setOnMenuItemClickListener {
                    val hourBefore = DateUtils.covertFormCurrentDate(DateUtils.COVENT_MONTH)
                    onNavigationItemClickListener.makeFeedAsRead(feedId,hourBefore)
                    true
                }
                menu.findItem(R.id.action_before_read_custom).setOnMenuItemClickListener {
                    true
                }
            }
        }
    }
}
